package com.dcxs.shop.module.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dcxs.shop.common.annotation.UnAuthRequest;
import com.dcxs.shop.common.utils.CheckoutUtil;
import com.dcxs.shop.common.utils.R;
import com.dcxs.shop.common.utils.ValidatorUtils;
import com.dcxs.shop.module.user.entity.JsUsr;
import com.dcxs.shop.module.user.entity.JsUsrAddress;
import com.dcxs.shop.module.user.service.IJsUsrAddressService;
import com.dcxs.shop.module.user.service.IJsUsrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * <p>
 * 用户收获地址表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-02-22
 */
@RestController
@RequestMapping("/user/js-usr-address")
public class JsUsrAddressController {

    @Autowired
    private IJsUsrService usrService;

    @Autowired
    private IJsUsrAddressService usrAddressService;

    @GetMapping("/myAddressList")
    public R myAddressList(HttpServletRequest request){
        String phone = request.getAttribute("userId").toString();
        JsUsr usr = usrService.getOne(new QueryWrapper<JsUsr>().eq("phone",phone));
        if(null!=usr){
            List<JsUsrAddress>  jsUsrAddresses = usrAddressService.list(new QueryWrapper<JsUsrAddress>().eq("usr_id",usr.getId()));
            return new R().put("data",jsUsrAddresses);
        }else {
            return R.error("没有此用户");
        }
    }

    @PostMapping("/saveOrUpdate")
    public R add(@RequestBody JsUsrAddress jsUsrAddress,HttpServletRequest request){
        ValidatorUtils.validateEntity(jsUsrAddress);
        String phone = request.getAttribute("userId").toString();
        JsUsr usr = usrService.getOne(new QueryWrapper<JsUsr>().eq("phone",phone));
        if(null==usr){
            return R.error("该用户需要注册");
        }
        if(1==jsUsrAddress.getState()){
            UpdateWrapper<JsUsrAddress> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("state",0).eq("usr_id",usr.getId());
            usrAddressService.update(updateWrapper);
        }
        jsUsrAddress.setUsrId(usr.getId());
        usrAddressService.saveOrUpdate(jsUsrAddress);
        return R.ok();
    }

    @PostMapping("/delete")
    public R delete(@RequestBody List<String> ids){
        usrAddressService.removeByIds(ids);
        return R.ok();
    }

    @RequestMapping("/token")
    @UnAuthRequest
    public void token(HttpServletRequest request, HttpServletResponse response) {
        boolean isGet = request.getMethod().toLowerCase().equals("get");
        PrintWriter print;
        if (isGet) {
            // 微信加密签名
            String signature = request.getParameter("signature");
            // 时间戳
            String timestamp = request.getParameter("timestamp");
            // 随机数
            String nonce = request.getParameter("nonce");
            // 随机字符串
            String echostr = request.getParameter("echostr");
            // 通过检验signature对请求进行校验，若校验成功则原样返回echostr，表示接入成功，否则接入失败
            if (signature != null && CheckoutUtil.checkSignature(signature, timestamp, nonce)) {
                try {
                    print = response.getWriter();
                    print.write(echostr);
                    print.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

}
